const ObsClient = require('esdk-obs-nodejs');
const path = require('path')
var obsClient = new ObsClient({
    access_key_id: '2A33VMC62WMLWFDBQDNC',
    secret_access_key: 'zyaRSs2WjtF0oxsnzwBKQp8RENyTv2TEOWQvRaTY',
    server: 'https://obs.cn-north-4.myhuaweicloud.com'
});

class obsTool {
    static download(objectname) {
        obsClient.getObject({
            Bucket: 'copy-right-content',
            Key: objectname,
            SaveAsFile: path.join(__dirname,'../public/images/') + objectname
        }, (err, result) => {
            if (err) {
                console.error('Error-->' + err);
            } else {
                console.log('Status-->' + result.CommonMsg.Status);
            }
        });
    }

    static upload(objectpath, objectname) {
        obsClient.putObject({
            Bucket: 'copy-right-content',
            Key: objectname,
            SourceFile: objectpath
        }, (err, result) => {
            if (err) {
                console.error('Error-->' + err);
            } else {
                console.log('Status-->' + result.CommonMsg.Status);
            }
        });
    }

    static delete(objectname) {
        obsClient.deleteObject({
            Bucket: 'copy-right-content',
            Key: objectname
        }, (err, result) => {
            if (err) {
                console.log('Error-->' + err);
            } else {
                console.log('Status-->' + result.CommonMsg.Status);
            }
        });
    }
}

module.exports = obsTool;